import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios"
// import { includes } from 'core-js/fn/array'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    titleList: [],  //一级表头
    itemList: [],  // 筛选出来的列表内容
    kwshowlist: [],  //筛选出来的关键字
    includesList:JSON.parse(localStorage.getItem('includesList'))|| []  //搜索的保存内容
  },
  mutations: {
    TITLE(state, item) {
      state.titleList = item
    },
    BOXITEM(state, item) {
      state.itemList = item
    },
    KWSSHOW(state, value) {
      state.kwshowlist = []
      state.itemList.forEach((ele) => {
        if (ele.title.includes(value)) {
          state.kwshowlist.push(ele)
        }
      })
      console.log(state.kwshowlist);
    },
    // 回车事件
    SEARCHLIST(state, value) {
      var indecindex = state.includesList.findIndex((ele) => {
        return ele === value
      })
      if(indecindex != -1){
        state.includesList.splice(indecindex,1)
      }
      state.includesList.unshift(value)
    
      localStorage.setItem('includesList',JSON.stringify(state.includesList))
    }
  },
  actions: {
    TITLE(context) {
      axios.get('./fenlei.json').then((res) => {
        console.log(res.data.result);
        context.commit('TITLE', res.data.result)
      }).catch((err) => {
        console.log(err);
      })
    },
    BOXITEM(context) {
      axios.get('./list.json').then((res) => {
        console.log(res.data.result);
        context.commit('BOXITEM', res.data.result)
      }).catch((err) => {
        console.log(err);
      })
    },
    KWSSHOW(context, value) {
      context.commit('KWSSHOW', value)
    },
    SEARCHLIST(context, value) {
      context.commit('SEARCHLIST', value)
    }
  },
  modules: {
  }
})
